package com.inossem.warehousestorage_inossem.activity.system;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.base.PingRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

public class SystemSettingActivity extends BaseActivity {

    TextView title;
    EditText domain, port;
    Button logout;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_system_setting);
        initView(SystemSettingActivity.this);
        initData(SystemSettingActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        domain = (EditText) activity.findViewById(R.id.domain);
        port = (EditText) activity.findViewById(R.id.port);
        logout = (Button) activity.findViewById(R.id.logout);
        ActivityUtils.initBackAndFunction(activity, "主页", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                save(activity);
            }
        });
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_system_setting_title));
        domain.setText(SPUtils.getDomain(activity));
        port.setText(SPUtils.getPort(activity));
        logout.setVisibility(View.GONE);
        logout.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                //登出
//                clear(activity);
//                WarehouseStorageApplication.getInstance().removeAllActivity();
//                startActivity(new Intent(activity, LoginActivity.class));
            }
        });
    }

//    private void clear(Context context) {
//        SPUtils.clearSystem(context);
//        SPUtils.clearWarehouseStorage(context);
//    }

    private String check(Context context) {
        if (TextUtils.isEmpty(domain.getText().toString())) {
            return "域名不可以为空";
        }
        if (TextUtils.isEmpty(port.getText().toString())) {
            return "端口不可以为空";
        }
        return null;
    }

    private void save(Context context) {
        String result = check(context);
        if (result == null) {
            String domainStr = domain.getText().toString().trim();
            String portStr = port.getText().toString().trim();
            PingRequest.request(context, domainStr, portStr, new GsonBeanListener() {
                @Override
                public void onSuccess(Object bean) {
                    SPUtils.saveDomain(context, domain.getText().toString().trim());
                    SPUtils.savePort(context, port.getText().toString().trim());
                    Utils.middleToast(context, "设置成功\n域名：" + domainStr + "\n端口：" + portStr);
                    setResult(Activity.RESULT_OK);
                    finish();
                }

                @Override
                public void onError(Context context, String code, String msg) {
                    Utils.middleToast(context, "域名和端口设置失败，请检查");
                }

                @Override
                public boolean useDefaultError() {
                    return false;
                }
            });
        } else {
            Utils.middleToast(context, result);
        }
    }

    @Override
    public void onBackPressed() {
        save(SystemSettingActivity.this);
    }
}
